<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增地库')"/>
    <th:block th:include="include :: datetimepicker-css"/>
    <th:block th:include="include :: select2-css"/>
</head>
<style>
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }

    input[type="number"] {
        -moz-appearance: textfield;
    }
</style>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-durationEffectDown-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">工程公司：</label>
            <div class="col-sm-8">
                <input type="hidden" name="companyId" th:value="${company.companyId}">
                <input class="form-control" type="text" name="companyName" th:value="${company.compName}" readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">项目名称：</label>
            <div class="col-sm-8">
                <input type="hidden" name="proId" th:value="${project.proId}">
                <input class="form-control" type="text" name="proName" th:value="${project.proName}" readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">第一块地库垫层完成时间：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" id="firstEndTime" name="firstEndTime">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">最后块地库顶板完成时间：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" id="lastEndTime" name="lastEndTime">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">用时：</label>
            <div class="col-sm-8">
                <input name="useTime" class="form-control" type="number" readonly>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">正负零完成后地库封闭天数：</label>
            <div class="col-sm-8">
                <input type="hidden" id="zeroEndTime" th:value="${zeroEndTime}">
                <input name="closeDay" class="form-control" type="number" readonly>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: datetimepicker-js"/>
<script>
    var prefix = ctx + "durationEffect";

    $(function () {
        dateLayui();
        computeTime();
    });

    function computeTime() {
        var firstEndTime = $("#firstEndTime").val();
        var lastEndTime = $("#lastEndTime").val();
        var zeroEndTime = $("#zeroEndTime").val();
        setNameVal("useTime", timeDifference(firstEndTime, lastEndTime));
        setNameVal("closeDay", timeDifference(lastEndTime, zeroEndTime));
    }

    function timeDifference(str, end) {
        var num = Number((new Date(end) - new Date(str)) / 1000 / 60 / 60 / 24 + 1)
        return isNaN(num) ? 0 : num;
    }

    function setNameVal(name, val) {
        $("input[name='" + name + "']").val(val);
        return val;
    }

    function dateLayui() {
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            var firstEndTime = laydate.render({
                elem: '#firstEndTime',
                type: 'date',
                done: function (value, date) {
                    computeTime();
                    if (value !== '') {
                        lastEndTime.config.min.year = date.year;
                        lastEndTime.config.min.month = date.month - 1;
                        lastEndTime.config.min.date = date.date;
                    } else {
                        lastEndTime.config.min.year = '';
                        lastEndTime.config.min.month = '';
                        lastEndTime.config.min.date = '';
                    }
                }
            });
            var lastEndTime = laydate.render({
                elem: '#lastEndTime',
                type: 'date',
                done: function (value, date) {
                    computeTime();
                    if (value !== '') {
                        firstEndTime.config.max.year = date.year;
                        firstEndTime.config.max.month = date.month - 1;
                        firstEndTime.config.max.date = date.date;
                    } else {
                        firstEndTime.config.max.year = '';
                        firstEndTime.config.max.month = '';
                        firstEndTime.config.max.date = '';
                    }
                }
            });
        });
    }

    function submitHandler() {
        if ($.validate.form()) {
            var data = $("#form-durationEffectDown-add").serializeArray();
            console.log(data);
            $.operate.save(prefix + "/saveDown", data);
        }
    }

</script>
</body>
</html>